package tfd.scala.squib.demo

import tfd.scala.squib._
import tfd.scala.squib.event._

import java.awt.{Component, EventQueue}
import java.awt.GridBagConstraints._
import java.awt.event._
import javax.swing.{DefaultComboBoxModel, DefaultListModel, JDialog, JList, JFrame, JTree, WindowConstants}
import javax.swing.event._
import javax.swing.tree.DefaultMutableTreeNode


object AllComponentsTree extends Application {
    
    class LabelledBlock(label:String, block: => Unit) {
        override def toString = label
        def invoke = block
    }

    lazy val desktop = desktoppane.id("desktop")
   
    def mousePanel:Component = panel(
            	'layout->borderlayout(),
                contents(
                	label("mousePanelLabel", 'text->" ") -> "South"
                ),
                mousePressed {
                    label.id("mousePanelLabel").setText("Mouse Pressed")
                },
                mouseMoved {
                    label.id("mousePanelLabel").setText("Mouse Moved")
                },
                mouseDragged {
                    label.id("mousePanelLabel").setText("Mouse Dragged")
                },
                mouseWheelMoved {
                	label.id("mousePanelLabel").setText("Mouse Wheel Moved")
                }
                
            )
            
    def simpleList = {
        val model = new DefaultListModel()
        model.addElement("Moe")
        model.addElement("Larry")
        model.addElement("Curly")
        lazy val stoogesList:JList = list("simpleList", 'model->model,
                valueChanged { label.id("simpleListLabel").setText("Selected: " + stoogesList.getSelectedValue.toString) } )
        panel(
                 'layout->borderlayout(),
                 contents(
                         stoogesList -> "Center",
                         label("simpleListLabel", 'text->" ") -> "South"
       			 )
         )
    }
    
    def borderLayout:Component = panel(
                                   'layout->borderlayout(),
                                   contents(
                                           button('text -> "North") -> "North",
                                           button('text -> "South") -> "South",
                                           button('text -> "East") -> "East",
                                           button('text -> "West") -> "West",
                                           button('text -> "Center") -> "Center"
                                 ))
     
                                 
    def radioButtons:Component = {
        val pnl = panel('layout -> gridlayout('columns->1, 'rows->5),
                        contents(
                           	label('text-> "<html><b>NFC West</b></html>"),
                            radiobutton("Rams", 'text->"Rams"),
                            radiobutton("Seahawks", 'text->"Seahawks"),
                            radiobutton("49ers", 'text->"49ers"),
                            radiobutton("Cardinals", 'text->"Cardinals")          
                        )
                  )
        buttongroup(
                radiobutton.id("Rams"),
                radiobutton.id("Seahawks"),
                radiobutton.id("49ers"),
                radiobutton.id("Cardinals")
        )
        pnl
    }
    
    def comboBoxes:Component = {
    	panel(
                contents(
                        combobox("simpsons", 'model -> new DefaultComboBoxModel(List("Homer","Marge","Bart","Lisa","Maggie").toArray.asInstanceOf[Array[Object]]),
                                itemStateChanged {
                            		label.id("comboBoxesItem").setText(combobox.id("simpsons").getSelectedItem.toString)
                                }
                        ),
                        label("comboBoxesItem", 'text->"?")
                )
        )
                                
    }
    
    def gridBagLayout:Component = {
        val gbc = gridbagconstraints('fill -> BOTH, 'weightx -> 1.0, 'weighty-> 0.0)
        panel(
                'layout->gridbaglayout(),
                contents(
                    button('text ->"Button1") -> gbc, 
                    button('text ->"Button2") -> gbc,
                    button('text ->"Button3") -> gbc,
                    button('text ->"Button4") -> gridbagconstraints(gbc, 'gridwidth -> REMAINDER), 
                    button('text ->"Button5") -> gridbagconstraints(gbc, 'weightx -> 0.0),
                    button('text ->"Button6") -> gridbagconstraints(gbc, 'gridwidth -> RELATIVE), 
                    button('text ->"Button7") -> gridbagconstraints(gbc, 'gridwidth -> REMAINDER), 
                    button('text ->"Button8") -> gridbagconstraints(gbc, 'gridheight -> 2, 'weighty -> 1.0),
                    button('text ->"Button9") -> gridbagconstraints(gbc, 'gridwidth -> REMAINDER),
                    button('text ->"Button10") -> gridbagconstraints(gbc, 'gridwidth -> REMAINDER)
                )
            )
    
    }
    
    def createlaunchnode(label:String, func:() => Component) 
     =  defaultmutabletreenode(
                'userobject-> new LabelledBlock(label, {
                    val frm =  internalframe('resizable -> true,
                                             'maximizable -> true,
                                             'iconifiable -> true,
                                             'visible -> true,
                                             'closable -> true,
                                             'location -> point(0,0),
                                             'size -> dimension(400, 300),
                                        contents(
                                             func()
                                        ),
                                        internalFrameActivated { System.out.println("Internal Frame Activated")},
                                        internalFrameClosed { System.out.println("Internal Frame Closed")},
                                        internalFrameClosing { System.out.println("Internal Frame Closing")},
                                        internalFrameDeactivated { System.out.println("Internal Frame Deactivated")},
                                        internalFrameDeiconified { System.out.println("Internal Frame Deiconified")},
                                        internalFrameIconified { System.out.println("Internal Frame Iconified")},
                                        internalFrameOpened { System.out.println("Internal Frame Opened")},
                                        componentResized { System.out.println("Component Resized") },
                                        componentMoved { System.out.println("Component Moved") },
                                        componentHidden { System.out.println("Component Hidden") },
                                        componentShown { System.out.println("Component Shown") }
                               )
                    desktop.add(frm)
                    desktop.moveToFront(frm)
                })
        )
    
    lazy val treePopup = popup(
            			contents(
                                menuitem('text->"Run")
                                )
                    )
    
    lazy val confirmClose:JDialog = dialog("dialog",
                'title -> "Confirm Close",
                'modal -> true,
                'layout->borderlayout(),
                contents(
                        label("Are you sure ?") -> "North",
                        panel('layout->flowlayout(),
                              contents(button('text->"Yes", actionPerformed{ System.exit(0) }),
                                       button('text->"No",  actionPerformed{ confirmClose.hide }))) -> "Center"
                )
        )
                    
                   
    lazy val mainFrame:JFrame = frame(
            'defaultCloseOperation -> WindowConstants.DISPOSE_ON_CLOSE,
            'size -> dimension(800,600),
            'visible->true,
            'jmenubar -> menubar(
                    contents(
                        menu(
                            'text-> "File",
                            'mnemonic->'F',
                            contents(
                                menuitem('text->"Close",
                                         'mnemonic -> 'C',
                                	actionPerformed {
                                    	val frameBounds = mainFrame.getBounds()
                                        confirmClose.setBounds(frameBounds.x + 100,frameBounds.y + 200, frameBounds.width - 200, 75)
                                        confirmClose.show
                                    }
                                )
                            )
                        )
                    )
            ),
            contents(
            	splitpane(
                	'leftComponent -> tree(
                         'rootVisible->false,
                         'model -> defaulttreemodel(defaultmutabletreenode(
                                 	 contents(
                                             defaultmutabletreenode(
                                                     'userobject -> "Layout Managers",
                                                     contents(
                                                             createlaunchnode("Border Layout", &borderLayout),
                                                             createlaunchnode("Grid Bag Layout", &gridBagLayout)
                                                     )
                                             ),
                                             createlaunchnode("Combo Box", &comboBoxes),
                                             createlaunchnode("Mouse Panel", &mousePanel),
                                             createlaunchnode("Radio Buttons", &radioButtons),
                                             createlaunchnode("Simple List", &simpleList)
                                      ))),
                         mouseClicked { me:MouseEvent => 
                             if (me.getClickCount == 2) {
                                val tree = me.getSource.asInstanceOf[JTree]
                                val treePath = tree.getPathForLocation(me.getX(), me.getY())
                                if(treePath != null) {
                                    val treeNode =  treePath.getLastPathComponent()
                                                            .asInstanceOf[DefaultMutableTreeNode]
                                    if (treeNode.isLeaf()) {
                                        val userObject = treeNode.getUserObject()
                                                             .asInstanceOf[LabelledBlock]
                                        if (userObject != null) {
                                            userObject.invoke 
                                        }
                                    }
                                }
                             }
                         },
                         mouseReleased { me:MouseEvent => 
                             if (me.isPopupTrigger) {
                                 treePopup.show(me.getSource.asInstanceOf[Component], me.getX(), me.getY())                                 
                             }
                         }
                     ),
                	'rightComponent -> desktoppane("desktop")
                )
            ),
            windowActivated { System.out.println("Window Activated") },
            windowClosed { 
                System.out.println("Window Closed")
                System.out.println("Exiting...")
                System.out.flush()
                System.exit(0)
            },
            windowClosing { System.out.println("Window Closing") },
            windowDeactivated { System.out.println("Window Deactivated") },
            windowDeiconified { System.out.println("Window Deiconified") },
            windowIconified { System.out.println("Window Iconified") },
            windowOpened { System.out.println("Window Opened") }
    )
    mainFrame
}
